﻿@model IEnumerable<SmallJob.Model.User>
@{
    var tablename = "TableUser";
    var numbercolumn = 5;
    var colsname = new[] { "ProperName", "UserName", "Email","Premium", "" };
    var curaction = "Index";
    var curcontroller = "User";
    var configaction = new { details = "Details", del = "Lock", create = "Create", undel = "UnLock" };
    var configtext = new { details = "Details", del = "Lock", create = "Create", undel = "UnLock" };
    var isfilter = ViewData["filter"] == null ? false : true;
}
<br />
<input type="text" name="search" value="@(isfilter?ViewData["filter"]:"")" id="inputsearch" /> Search Here, Type and Enter
<div class="div-parent">
    
    <table class="table table-hover sort table-bordered">
        <thead>
            <tr>
                @for (int i = 0; i < numbercolumn; i++)
                {
                    <th>@colsname[i]
                        <span style="float: right;"><i class="icon-sort"></i></span>
                    </th>
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProperName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.UserName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Email)
                    </td>
                    <td>
                        @if (item.PremiumTo == null) { 
                            <span class="label label-inverse">Chưa</span>
                        }else{
                            if (!item.Premium) { 
                                <span class="label">@item.PremiumTo</span>
                            }else{
                                <span class="label label-success">@item.PremiumTo</span>
                            }
                        }
                    </td>
                    <td>
                        @Html.ActionLink(@configtext.details, configaction.details, new { id = item.ID }) |
                        @if (item.IsLock)
                        {
                            <a onclick="undel(this,@item.ID)" href="javascript:;">@configtext.undel</a>
                        }
                        else
                        {
                            <a  onclick="del(this,@item.ID)" href="javascript:;">@configtext.del</a>
                        }

                    </td>
                </tr>
            }
        </tbody>

    </table>

    <div class="pagination">
        <ul>
            @{
                var total = Convert.ToInt32(ViewData["total"]);
                var curpage = Convert.ToInt32(ViewData["curpage"]);
                if (curpage > 1)
                {
                <li><a href="/@curcontroller/@curaction/pages/@(curpage - 1)/filter/@(isfilter ? ViewData["filter"] : "")">Prev</a></li>
                }
                for (int i = 1; i <= total; i++)
                {
                    if (i != curpage)
                    {
                <li><a href="/@curcontroller/@curaction/pages/@(i)/filter/@(isfilter ? ViewData["filter"] : "")">@i</a></li>
                    }
                    else
                    {
                <li><a href="/@curcontroller/@curaction/pages/@(i)/filter/@(isfilter ? ViewData["filter"] : "")"><strong>@i</strong></a></li>
                    }

                }
                if (curpage < total)
                {
                <li><a href="/@curcontroller/@curaction/pages/@(curpage + 1)/filter/@(isfilter ? ViewData["filter"] : "")">Next</a></li>
                }
            }


        </ul>
    </div>
</div>

<script src="@Url.Content("~/Content/js/jquery.tablesorter.min.js")"></script>
<script>
    //table sort
    $(function () {
        $("table.sort").tablesorter();
    });
    $("table").bind("sortStart", function () {
    }).bind("sortEnd", function () {
        $('th.header').each(function () {
            if ($(this).hasClass('headerSortDown')) {
                $(this).find('.icon-sort').addClass('icon-hand-up');
                $(this).find('.icon-sort').removeClass('icon-hand-down');

            }
            if ($(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').addClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
            if (!$(this).hasClass('headerSortDown') && !$(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').removeClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
        });
    });
    //del and undel ajax
    function del(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(configaction.del, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.undel');
                    $(source).attr('onClick', 'undel(this,' + id + ')');
                } else {
                }
            }
        });
    }
    function undel(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(@configaction.undel, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.del');
                    $(source).attr('onClick', 'del(this,' + id + ')');
                } else {
                }
            }
        });
    }
    $(document).ready(function () {
        $('#inputsearch').change(function () {
            var filter = $(this).val();
            $.ajax({
                type: "POST",
                url: '@Url.Action(@tablename, curcontroller)',
                data: { target: 1 , page: 1, pagesize: 1, filter: filter },
                success: function (data) {
                    if (data != null) {
                        var getdata = $(data).filter('.div-parent');
                        $('.div-parent').html(getdata.html());
                    } else {
                    }
                }
            });
        });
    });
</script>
